#******************************************************************************
#******************************************************************************

from src.hyhetra.common import factors

from src.hyhetra.common.formulas import HeatTransferRateThermalResistance

from numpy.testing import assert_allclose

#******************************************************************************
#******************************************************************************

def examples():
    
    example_cengel2003_3dash13()
    
    example_cengel2003_3dash14()

#******************************************************************************
#******************************************************************************

def example_cengel2003_3dash13():

    #**************************************************************************
    #**************************************************************************
    
    # Source: 
    # Cengel. Y, Heat Transfer: A Practical Approach, 2003, pp. 170, ex. 3-13
    
    temperature_ground_surface = 10+273.15 # K
    
    temperature_pipe_surface = 80+273.15 # K
    
    pipe_length = 30 # m
    
    pipe_diameter = 0.1 # m
    
    pipe_depth = 0.5 # m
    
    soil_k = 0.9 # W/mK
    
    s_true = 62.9 # m
    
    q_true = 3963 # W
    
    # tolerance
    
    relative_tolerance = 0.01
    
    #**************************************************************************
    #**************************************************************************
    
    ( 
     shape_conduction_factor
     ) = factors.shape_factor_buried_horizontal_isothermal_cylinder(
        pipe_diameter,
        pipe_length,
        pipe_depth)
    
    ( 
     thermal_resistance
     ) = factors.thermal_resistance_from_shape_factor(
        shape_conduction_factor,
        soil_k)
    
    # heat_transfer_rate = (
    #     temperature_pipe_surface-temperature_ground_surface)/thermal_resistance
    
    heat_transfer_rate = HeatTransferRateThermalResistance(
        temperature_pipe_surface-temperature_ground_surface,
        thermal_resistance)
    
    #**************************************************************************
    #**************************************************************************
    
    # shape factor
    
    assert_allclose(shape_conduction_factor, 
                    s_true,
                    rtol=relative_tolerance)
    
    # heat transfer rate
                    
    assert_allclose(heat_transfer_rate, 
                    q_true,
                    rtol=relative_tolerance)

    #**************************************************************************
    #**************************************************************************
    
    # trigger warnings
    
    # short length
    
    error_raised = False
    try:
        ( 
         shape_conduction_factor
         ) = factors.shape_factor_buried_horizontal_isothermal_cylinder(
            pipe_diameter,
            pipe_length*0.001,
            pipe_depth)
    except Warning:
        error_raised = True
    assert error_raised
    
    # shallow depth
    
    try:
        ( 
         shape_conduction_factor
         ) = factors.shape_factor_buried_horizontal_isothermal_cylinder(
            pipe_diameter,
            pipe_length,
            pipe_depth*0.001)
    except Warning:
        error_raised = True
    assert error_raised
    
    #**************************************************************************
    #**************************************************************************

#******************************************************************************
#******************************************************************************

def example_cengel2003_3dash14():

    #**************************************************************************
    #**************************************************************************
    
    # Source: 
    # Cengel. Y, Heat Transfer: A Practical Approach, 2003, pp. 173, ex. 3-14
    
    temperature_pipe_cold = 15+273.15 # K
    
    temperature_pipe_hot = 70+273.15 # K
    
    pipe_length = 5 # m
    
    pipe_diameter = 0.05 # m
    
    pipe_distance = 0.30 # m
    
    soil_k = 0.75 # W/mK
    
    s_true = 6.34 # m
    
    q_true = 262 # W
    
    # tolerance
    
    relative_tolerance = 0.01
    
    #**************************************************************************
    #**************************************************************************
    
    ( 
     shape_conduction_factor
     ) = factors.shape_factor_two_parallel_isothermal_cylinders(
        pipe_diameter,
        pipe_diameter,
        pipe_distance,
        pipe_length)
    
    ( 
     thermal_resistance
     ) = factors.thermal_resistance_from_shape_factor(
        shape_conduction_factor,
        soil_k)
    
    # heat_transfer_rate = (
    #     temperature_pipe_hot-temperature_pipe_cold)/thermal_resistance
    
    heat_transfer_rate = HeatTransferRateThermalResistance(
        temperature_pipe_hot-temperature_pipe_cold,
        thermal_resistance)
    
    #**************************************************************************
    #**************************************************************************
    
    # shape factor
    
    assert_allclose(shape_conduction_factor, 
                    s_true,
                    rtol=relative_tolerance)
    
    # heat transfer rate
                    
    assert_allclose(heat_transfer_rate, 
                    q_true,
                    rtol=relative_tolerance)

    #**************************************************************************
    #**************************************************************************
    
    # trigger warnings
    
    # short length relative to pipe diameters
     
    error_raised = False
    try:
        ( 
         shape_conduction_factor
         ) = factors.shape_factor_two_parallel_isothermal_cylinders(
            pipe_diameter,
            pipe_diameter,
            pipe_distance*0.001,
            pipe_length*0.001)
    except Warning:
        error_raised = True
    assert error_raised
    
    # short length relative to interpipe distance
    
    error_raised = False
    try:
        ( 
          shape_conduction_factor
          ) = factors.shape_factor_two_parallel_isothermal_cylinders(
            pipe_diameter,
            pipe_diameter,
            pipe_distance*1e3,
            pipe_length)
    except Warning:
        error_raised = True
    assert error_raised

    #**************************************************************************
    #**************************************************************************

#******************************************************************************
#******************************************************************************